package com.byteshake.ddoffice.ui.home

import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import androidx.core.view.children
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.alibaba.android.arouter.facade.annotation.Route
import com.byteshake.common.PageRouters
import com.byteshake.ddoffice.R
import com.byteshake.common.base.BaseBindingActivity
import com.byteshake.ddoffice.databinding.ActivityHomeBinding
import com.byteshake.common.navigator.FixFragmentNavigator


@Route(path = PageRouters.App_Home)
class HomeActivity : BaseBindingActivity<ActivityHomeBinding>({
    ActivityHomeBinding.inflate(it)
}) {


    private lateinit var navController: NavController

    private val fragmentUI: List<Fragment> by lazy {
        listOf(TestUIFragment().setTitle("消息"),
                TestUIFragment().setTitle("好友"),
                TestUIFragment().setTitle("发现"),
                TestUIFragment().setTitle("我的"))
    }
    private var currentFragment: Fragment? = null

    override fun initView(savedInstanceState: Bundle?) {
//        navController = Navigation.findNavController(this, R.id.nav_host_fragment)
//        val fragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
//        val fragmentNavigator = FixFragmentNavigator(this, supportFragmentManager, fragment.id)
//        navController.navigatorProvider.addNavigator(fragmentNavigator)
//        navController.setGraph(R.navigation.nav_graph)
        val bottomNav = binding.navView
        bottomNav.itemIconTintList = null
//        bottomNav.setupWithNavController( navController)

//        val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
//        binding.navView.setupWithNavController(navHostFragment.navController)

        //屏蔽长按吐司
        (bottomNav.getChildAt(0) as? ViewGroup)?.children?.forEach { it.setOnLongClickListener { true } }

//        supportFragmentManager.commit {
////            binding.navFragmentContainer.
//            replace(R.id.nav_fragment_container, TestUIFragment())
//        }

        // 设置导航项点击事件监听器
        bottomNav.setOnItemSelectedListener { item ->
            Log.d(TAG, "导航项点击事件监听器: " + item.title)
            when (item.itemId) {
                R.id.nav_msg -> {
                    switchFragment(HomeFragmentTag.HOME_MSG_TAG.tag) { fragmentUI[0] }
                    true
                }
                R.id.nav_friend -> {
                    switchFragment(HomeFragmentTag.HOME_FRIEND_TAG.tag) { fragmentUI[0] }
                    true
                }
                R.id.nav_discover -> {
                    switchFragment(HomeFragmentTag.HOME_DISCOVER_TAG.tag) { fragmentUI[0] }
                    true
                }
                R.id.nav_mine -> {
                    switchFragment(HomeFragmentTag.HOME_MINE_TAG.tag) { fragmentUI[0] }
                    true
                }
                else -> false
            }
        }

    }

    private fun switchFragment(fragmentTag: String, creator: () -> Fragment) {
        val fragmentManager = supportFragmentManager
        val fragment = fragmentManager.findFragmentByTag(fragmentTag) ?: creator()
        if (fragment !== currentFragment) {
            fragmentManager.beginTransaction()
                .replace(R.id.nav_fragment_container, fragment, fragmentTag)
                .commit()
            currentFragment = fragment
        }
    }


    override fun initData(savedInstanceState: Bundle?) {

    }
}